package down.xiaoheihe.work.Controller;

import down.xiaoheihe.work.Entity.TOrder;
import down.xiaoheihe.work.Entity.TProduct;
import down.xiaoheihe.work.Entity.User;
import down.xiaoheihe.work.Mapper.TOrderMapper;
import down.xiaoheihe.work.Mapper.TProductMapper;
import down.xiaoheihe.work.Mapper.TimagesMapper;
import down.xiaoheihe.work.Mapper.UserMapper;
import down.xiaoheihe.work.Response.orderR;
import down.xiaoheihe.work.Response.orderT;
import down.xiaoheihe.work.Service.service3;
import down.xiaoheihe.work.utils.ElasticUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@MapperScan("down.xiaoheihe.work.Mapper")
@CrossOrigin
public class OdersController {
    @Autowired
    TProductMapper tProductMapper;
    @Autowired
    TOrderMapper tOrderMapper;
    @Autowired
    service3 service3;
    @Autowired
    ElasticUtils utils;

    @Autowired
    UserMapper userMapper;

    @GetMapping("/select")
    public List<Object> selectAll(@RequestParam Map<String, String> requestparam) throws IOException {
        String value = requestparam.get("value");
        if (value.length()>0) {
            return utils.find(value);
        } else {
            return utils.findAll();
        }
    }
    @GetMapping("/orders")
    public List<orderR> 查询我的所有订单() {
        Subject subject = SecurityUtils.getSubject();
        Integer id = (Integer) subject.getPrincipal();
        return service3.查询我的所有订单(id);
    }
    @GetMapping("/order")
    public orderT 查询指定订单(@RequestParam Integer id) {
        return service3.查询指定订单(id);
    }

    @GetMapping("/queren")
    public String 确认收货(@RequestParam Integer id) {
        TOrder tOrder = tOrderMapper.selectByPrimaryKey(id);
        tOrder.setStatus(2);
        tOrderMapper.updateByPrimaryKeySelective(tOrder);
        return "OK";
    }

    @PostMapping("/maijiajiaqian")
    public String 买家确认收货后卖家加钱(@RequestParam Integer id) {
        TOrder tOrder = tOrderMapper.selectByPrimaryKey(id);
        //卖家ID
        int maiJiaId = tOrder.getMaijiauid();

        //查询商品id，获取商品的单价，然后用租用天数乘以单价
        int productId = tOrder.getProductId();
        //获得商品
        TProduct tProduct = tProductMapper.selectByPrimaryKey(productId);
        //该商品的单价
        long price = tProduct.getPrice();
        //获得订单租用天数
        int day = tOrder.getDays();

        //天数乘单价
        long dayPrice = day * price;

        //获取卖家
        User user = userMapper.selectByPrimaryKey(maiJiaId);

        //更新余额
        long money = user.getBalance() + dayPrice;

        user.setBalance(money);

        userMapper.updateByPrimaryKeySelective(user);

        return "卖家已到帐";
    }


    @GetMapping("/queren2")
    public String 卖家确认收货(@RequestParam Integer id) {
        TOrder tOrder = tOrderMapper.selectByPrimaryKey(id);
        tOrder.setStatus(4);
        tOrderMapper.updateByPrimaryKeySelective(tOrder);

        //买家id
        int maiJiaId = tOrder.getUseruid();

        //查询商品id，获取商品的单价，然后用租用天数乘以单价
        int productId = tOrder.getProductId();
        //获得商品
        TProduct tProduct = tProductMapper.selectByPrimaryKey(productId);
        //该商品的押金
        long money = tProduct.getGuarentee();

        User user = userMapper.selectByPrimaryKey(maiJiaId);

        user.setBalance(user.getBalance() + money);

        userMapper.updateByPrimaryKeySelective(user);


        return "OK";
    }
    @PostMapping("/jihui")
    public String 我要寄回(@RequestParam Integer id, @RequestParam String wuliu) {

        TOrder tOrder = tOrderMapper.selectByPrimaryKey(id);
        tOrder.setStatus(3);
        tOrder.setJiwuliu(wuliu);
        tOrderMapper.updateByPrimaryKeySelective(tOrder);

        return "OK";
    }
    @PostMapping("/fahuo")
    public String 发货(@RequestParam Integer id, @RequestParam String wuliu) {
        TOrder tOrder = tOrderMapper.selectByPrimaryKey(id);
        tOrder.setStatus(1);
        tOrder.setFawuliu(wuliu);
        tOrderMapper.updateByPrimaryKeySelective(tOrder);
        return "OK";
    }
    @GetMapping("/rented")
    public List<orderR> 查询我卖的订单() {
        Subject subject = SecurityUtils.getSubject();
        Integer id = (Integer) subject.getPrincipal();
        return service3.查询我卖的订单(id);
    }
    @PostMapping("/createorder")
    public TOrder createOrder(@RequestBody TOrder tOrder) {
        tOrder.setCreateTime(new Date());
        TProduct tProduct = new TProduct();
        tProduct.setId(tOrder.getProductId());
        tProduct.setStatus(2);
        tProductMapper.updateByPrimaryKeySelective(tProduct);
        tOrderMapper.insert(tOrder);
        return tOrder;
    }
}
